//
//  TF_AdvertView.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2021/12/27.
//  Copyright © 2021 Talkfun. All rights reserved.
//

import UIKit

class TF_AdvertView: UIView {
    
    @objc public var videoView:UIView!//广告视频
    @objc public let back = TalkfunIncreaseButton.init()//返回键
    @objc public var secondLabel:UILabel!//秒
    @objc public var firstSecondLabel:UILabel!
    @objc public var prompt:TalkfunIncreaseButton!//路过片头
    @objc public let fullScreen = TalkfunIncreaseButton.init()//全屏
    var first:UIView!
    var skipLabel:UILabel! // 跳过片头
    var splitLine:UIView!;//分割线
    var S_Label:UILabel!//S

    var plitLine:UILabel! //分割线

    var backImage:UIImageView!;

    var first_S_Label:UILabel!
    var firstSplitLine:UIView!

    var skip:Bool!

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.videoView = UIView.init()
        self.addSubview(self.videoView)
    
        
        self.back.backgroundColor = UIColor(red: 17/255.0, green: 43/255.0, blue: 68/255.0, alpha: 0.7)
        self.addSubview(self.back)
     
        
        self.backImage = UIImageView.init()
        self.backImage.image = UIImage.init(named: "HTLive.bundle/back4 ")
        self.back.addSubview(self.backImage)

      
        self.prompt = TalkfunIncreaseButton.init()
      
        self.prompt.backgroundColor = UIColor(red: 17/255.0, green: 43/255.0, blue: 68/255.0, alpha: 0.7)
        self.prompt.layer.cornerRadius = 17.5;
        self.addSubview(self.prompt)
       
        
        self.skipLabel = UILabel.init()
        self.skipLabel.numberOfLines = 0;
        self.prompt.addSubview(self.skipLabel)
       
        

        self.skipLabel.text = "跳过片头";
     
        self.skipLabel.textColor = UIColor.white
        
        self.skipLabel.font = UIFont.PingFang(size: 16)
        //秒
        self.secondLabel = UILabel.init()
       
        self.secondLabel.numberOfLines = 0;
        self.prompt.addSubview(self.secondLabel)
       
    
        self.secondLabel.font = UIFont.PingFang(size: 16)
        
        self.secondLabel.textColor = UIColor(red: 255/255.0, green: 210/255.0, blue: 30/255.0, alpha: 1.0)
        //---
        //S
        self.S_Label = UILabel.init()
       
        self.S_Label.numberOfLines = 0;
        self.prompt.addSubview(self.S_Label)
        
   
        self.S_Label.text = "S";
        self.S_Label.textColor = UIColor.white
        self.S_Label.font = UIFont.PingFang(size: 16)
        
        
        
        self.splitLine = UIView.init()
        self.splitLine.backgroundColor = UIColor.white
        self.prompt.addSubview(self.splitLine)
     

        
        self.first = UIView.init()
        
        self.first.backgroundColor = UIColor(red: 17/255.0, green: 43/255.0, blue: 68/255.0, alpha: 0.7)
        
        self.first.layer.cornerRadius = 17.5;
        self.addSubview(self.first)
        
        
        //秒
        self.firstSecondLabel = UILabel.init()
        self.firstSecondLabel.text = "00";
        self.firstSecondLabel.numberOfLines = 0;
        self.first.addSubview(self.firstSecondLabel)
        self.firstSecondLabel.font = UIFont.PingFang(size: 16)
        self.firstSecondLabel.textColor =  UIColor(red: 255/255.0, green: 210/255.0, blue: 30/255.0, alpha: 1.0)
        //---
        //S
        self.first_S_Label = UILabel.init()
        
        self.first_S_Label.numberOfLines = 0;
        self.first.addSubview(self.first_S_Label)

        
        self.first_S_Label.text = "S";
        self.first_S_Label.textColor = UIColor.white
        self.first_S_Label.font = UIFont.PingFang(size: 16)
        
        //分割线
        self.firstSplitLine = UIView.init()
//        self.firstSplitLine.frame = CGRectMake(271,45,0.5,15);
        self.firstSplitLine.backgroundColor = UIColor.white
        self.first.addSubview(self.firstSplitLine)
        
 
        self.fullScreen.setImage(UIImage.init(named: "HTLive.bundle/Adfullscreen"), for: .normal)
        self.fullScreen.setImage(UIImage.init(named: "HTLive.bundle/Adfullsca"), for: .selected)
       
        self.fullScreen.backgroundColor = UIColor(red: 17/255.0, green: 43/255.0, blue: 68/255.0, alpha: 0.7)
        self.addSubview(self.fullScreen)
       
        
        self.secondLabel.textAlignment = .right
        self.firstSecondLabel.textAlignment = .right
    }
    func isPad() -> Bool {
     var pad = false as Bool
        if UIDevice.current.userInterfaceIdiom == .pad {
            pad = true
        }
      return pad

    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        var promptY:CGFloat = 0.0;
      
        
        if (self.isNotchScreen) {
            
            promptY = 34.0 + 15.0;
            print("刘海屏幕")
        }else if ( self.isPad())
                    
        {
            print("是IPAD")
            promptY = 34.0 + 15.0;
        } else{
            print("没刘海的手机")
            promptY = UIApplication.shared.statusBarFrame.size.height + 15.0
           
        }
        self.videoView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
 
        self.back.frame = CGRect(x: 15.0, y: promptY, width: 35.0, height: 35.0)
  
        self.back.layer.cornerRadius = 17.5;
        self.backImage.frame = CGRect(x: (self.back.frame.size.width - 9.3)/2, y: (self.back.frame.size.height - 16)/2, width: 9.3, height: 16)
  
        self.prompt.frame = CGRect(x: self.frame.size.width - 150-15, y: promptY, width: 146, height: 35)
      
        let W:CGFloat =  64 + 10 + 0.5 + 10 + 0.5 + 10 + 20
        
        let skipLabelX:CGFloat = (self.prompt.frame.size.width - W)/2.0 + 10 + 0.5 + 10 + 0.5 + 10 + 20
        //跳过片头
        self.skipLabel.frame = CGRect(x: skipLabelX, y: 0, width: 70, height: self.prompt.frame.size.height)
  
        //分割线
        
        self.splitLine.frame = CGRect(x: self.skipLabel.frame.origin.x - 10 - 0.5, y: 10, width: 0.5, height: 15)
        
        self.S_Label.frame = CGRect(x: self.skipLabel.frame.origin.x - 10 - 0.5, y: 0, width: 10, height: self.prompt.frame.size.height)
        self.secondLabel.frame = CGRect(x: self.S_Label.frame.origin.x-26, y: 11, width: 26, height: 12.5)
     
        self.first.frame = CGRect(x: self.frame.size.width - 58 - 15, y: promptY, width: 58, height: 35)
       
        let firstSecondLabelW:CGFloat = 26
        self.first_S_Label.frame = CGRect(x: (self.first.frame.size.width - 10 - firstSecondLabelW)/2 + firstSecondLabelW, y: 0, width: 10, height: self.prompt.frame.size.height)
        self.firstSecondLabel.frame = CGRect(x: self.first_S_Label.frame.origin.x - firstSecondLabelW, y: 11, width: firstSecondLabelW, height: 12.5)
        self.fullScreen.frame = CGRect(x: self.frame.size.width - 35 - 10, y: self.frame.size.height - 35 - 10, width: 35, height: 35)
        
        self.fullScreen.layer.cornerRadius = 35/2;
    }
    //开始播放
    @objc public func advertisingPlayStart(model:TalkfunADConfig,playbackID:String)  {
        let duration:NSInteger = model.duration
       
        self.skip = TFCourseManagement.skipAdvertising(Int(playbackID)!)
        //已经看完第一次广告,但是后台配置(isSkip)是不能跳过广告,所以不能跳过广告
        if(model.isSkip == false){
            self.skip = false
        }
       
        
        if (duration<10) {
            if (self.skip) {
            self.secondLabel.text = "0" + String(duration)
            self.first.isHidden = true
            }else{
                self.firstSecondLabel.text = "0" + String(duration)
                self.prompt.isHidden = true
              
            }
            
            
        }else{
            if (self.skip) {
                self.secondLabel.text = "0" + String(duration)
               
              self.first.isHidden = true
               
            }else{
                self.firstSecondLabel.text = String(duration)
               
                self.prompt.isHidden = true
               
            }
            
        }
    }
    @objc public func setSecond(duration:NSInteger,playbackID:String) {
        if (duration<10) {
            if (self.skip) {
                self.secondLabel.text = "0" + String(duration)
            }else{
                self.firstSecondLabel.text = "0" + String(duration)
            }
           
        }else{
              if (self.skip) {
                  self.secondLabel.text = String(duration)
              }else{
                    self.firstSecondLabel.text = String(duration)
                  
              }
          
        }
    }
    //广告播放完成
    @objc public func advertisingPlaybackCompleted(playbackID:String) {
        //保存可以跳过
       
        TFCourseManagement.saveSkip(Int(playbackID)!)
        self.isHidden = true;
        self.skip   = false;
    }
   
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
